//
//  DXTabBarController.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/5.
//  Copyright © 2015 simon. All rights reserved.
//

import UIKit

class DXTabBarController: UITabBarController{
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let dxtabBar = DXTabBar()
        dxtabBar.backgroundImage = UIImage(named: "tabbar_background")
    // MARK: -  自定义tabBar按钮事件的回调
        dxtabBar.compseBtnClickClosure = {
//        logPrint("UITabBarController")
          let compseView = DXCompaseView()
            compseView.show(self)
            
        }
//        kvc直接给只读属性重新赋值
        setValue(dxtabBar, forKeyPath: "tabBar")
        
//        添加子控制器
        addChildViewController(DXHomeViewController(), title: "首页", imgName: "tabbar_home")
        
         addChildViewController(DXMessageViewController(), title: "消息", imgName: "tabbar_message_center")
        
         addChildViewController(DXDiscoverViewController(), title: "发现", imgName: "tabbar_discover")
        
         addChildViewController(DXAboutViewController(), title: "我", imgName: "tabbar_profile")
    }
    
    
    // MARK: - 重载构造函数

   @objc private  func addChildViewController(childController: UIViewController, title:String ,imgName:String) {
        
        //        设置图片和文字渲染的颜色
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    
//        设置item的渲染颜色
//        childController.tabBarItem
//        设置item的标题
        childController.tabBarItem.title = title
//        设置item的图片
//        设置图片渲染的模式
        let img = UIImage(named:imgName)
    img?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        childController.tabBarItem.image = img
        
//        设置item被选中后的图片
        childController.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected")?.imageWithRenderingMode(.AlwaysOriginal)
//        将子控制器套一个navigation控制器
    let navigationVc = DXNavigationController(rootViewController: childController)
//        重构后调用原来的addChildViewController方法
        addChildViewController(navigationVc)
    }

    

}
